<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> 
<head>
<%@ include file="../../include/easyui.jsp" %>
<%@ include file="../../include/pGrid.jsp" %>
<script type="text/javascript" src="/plug-in/jquery-easyui-1.5.1/plugins/datagrid-cellediting.js"></script>
<style type="text/css">
	.toolbar{padding: 10px;}
	.toolbar .item{margin-top: 5px;}
	.easyui-linkbutton{width: 120px;}
</style>
<title>表单数据库表设计</title>
<script>
$(function() {
	var pFn = {
		formId: '${param.formId}',
		/*创建表*/
		saveConfig: function(){
			var _this = this;
			var $grid = $("#dataGrid");
			var rows = $grid.datagrid("getRows");
			for(var i=0,len=rows.length;i<len; i++){
				var index = $grid.datagrid("getRowIndex",rows[i]);
				$grid.datagrid("endEdit",index);
			}
			var data = $grid.datagrid("getData");
			var lineCount = $('#lineCount').combobox('getValue');
			data.lineCount = lineCount ;
			
			var tableConfig = JSON.stringify(data);
			
			var data = {'tableConfig':tableConfig,'formId':_this.formId} ;
			$.post('/console/jForm/rewrite/tableConfig', data ,function(res){
				if(res && res.flag){
					$grid.datagrid('reload'); 
					$.messager.show({
						title:'温馨提醒',
						msg:'设置成功！',
						timeout:5000,
						showType:'slide'
					});
				}else{
					alert('设置失败!');
				}
			});
			$grid.datagrid('clearSelections'); 
			return false;
		},
		frushConfig:function(){
			$("#dataGrid").datagrid('reload');    
			$("#dataGrid").datagrid('clearSelections'); 
		},
		viewConfig: function(){
			window.location.href = '/console/jForm/rewrite/review?formId='+ this.formId;
		}
	};
	
	/*左侧控件调用*/
	$(".easyui-linkbutton").on("click",function(){
		var $this = $(this);
		var id = $this.attr("id");
		if(id=='saveConfigBtn'){
			pFn.saveConfig();
		}
		if(id=='frushConfigBtn'){
			pFn.frushConfig();
		}
		if(id='viewConfigBtn'){
			pFn.viewConfig();
		}
	});
	
	/*表单属性列表加载*/
	$('#dataGrid').datagrid({    
		idField:'fieldname',
		singleSelect: false,
		rownumbers: true,
		sortName: 'fieldOrder',
       	sortOrder: 'asc',
       	remoteSort: false,
       	multiSort:true,
	    url:'/console/jForm/rewrite/getDbGrid?formId=${param.formId}',    
	    columns:[[ 
	        {field:'fieldName',title:'字段',width:60},  
	        {field:'fieldLanW',title:'控件类型',width:120,formatter:function(value,row,index){
	        	var fieldName = row.fieldName;
	        	if(fieldName=='id') return '主键';
	        	if(fieldName.indexOf('i')>-1){
	        		return '数字输入框' + fieldName.replace('i','');
	        	}
	        	if(fieldName.indexOf('v')>-1){
	        		return '普通文本输入框' + fieldName.replace('v','');
	        	}
	        	if(fieldName.indexOf('s')>-1){
	        		return '长文本输入框' + fieldName.replace('s','');
	        	}
	        }},    
	        {field:'fieldTitle',title:'字段名称',width:100,editor:{
	    		type: 'textbox', options:{ required: false, validType:['length[0,20]'] }
	     	}},    
	        {field:'fieldPlug',title:'类别',width:100,editor:{
		    	type:'combobox',
				options:{
					method:'get',
					url:'/console/combobox?valueField=dicName&textField=dicName&modleName=dictionary&pCode=C10&pCode_searchType=eq',
					required: false
				}
		    }},    
	        {field:'fieldEnabled',title:'启用',width:60,editor:{
		    	type:'combobox',
				options:{
					valueField:'text',    
				    textField:'text', 
				    data:[{'text':'Y'},{'text':'N'}],
					required: false
				}
		    }},    
		    {field:'fieldOrder',title:'排序',width:60,
		    	sortable:true,
		    	sorter: function(a,b){
		    		a = a || 1000000;
		    		b = b || 1000000;
		    		a = parseInt(a+'', 10);
		    		b = parseInt(b+'', 10);
		    		return a>b ? 1: -1;
		    	},
		    	editor:{
			    	type:'combobox',
					options:{
						valueField:'text',    
					    textField:'text', 
					    data:[{'text':'1'},{'text':'2'},{'text':'3'},{'text':'4'},{'text':'5'},{'text':'6'},{'text':'7'},{'text':'8'},{'text':'9'},{'text':'10'},{'text':'11'},{'text':'12'},{'text':'13'},{'text':'14'},{'text':'15'},{'text':'16'},{'text':'17'},{'text':'18'},{'text':'19'},{'text':'20'},{'text':'21'},{'text':'22'},{'text':'23'},{'text':'24'},{'text':'25'},{'text':'26'},{'text':'27'},{'text':'28'},{'text':'29'},{'text':'30'}],
						required: false
					}
		    }},    
		    /*{field:'fieldCol',title:'列宽',width:60,editor:{
		    	type:'combobox',
				options:{
					valueField:'text',    
				    textField:'text', 
				    data:[{"text":"1"},{"text":"2"},{"text":"3"},{"text":"4"},{"text":"5"}],
					required: false
				}
		    }},   
		    {field:'fieldLine',title:'强制换行',width:60,editor:{
		    	type:'combobox',
				options:{
					valueField:'text',    
				    textField:'text', 
				    data:[{'text':'Y'},{'text':'N'}],
					required: false
				}
		    }},
		    {field:'fieldValidType',title:'验证',width:160,editor:{
		    	type:'combobox',
				options:{
					multiple: true, valueField:'text', textField:'text', 
				    data:[
			          {'text':"'email'"},
			          {'text':"'url'"},
			          {'text':"'length[0,50]'" },
			          {'text':"'length[0,500]'" }
				    ],
					required: false
				}
		    }},*/
		    
	        {field:'fieldRequired',title:'属性',width:340,editor:{
		    	type:'combobox',
				options:{
					multiple: true,
					valueField:'text',    
				    textField:'text', 
				    groupField: "group",
				    data:[
			          {'text':"required:true",group:"验证属性"},
			          {'text':"min:0,precision:0",group:"数值属性"},
			          {'text':"min:0,precision:2",group:"数值属性"},
			          {'text':"readonly:true",group:"文本属性"},
			          {'text':"multiline:true",group:"文本属性"},
			          {'text':"height:60",group:"文本属性"},
			          {'text':"height:100",group:"文本属性"},
			          {'text':"disabled:true",group:"文本属性"},
			          {'text':"multiple:true",group:"下拉属性"},
			          {'text':"cascadeCheck:false",group:"下拉属性"},
			          {'text':"showSeconds:false",group:"时间属性"},
			          {'text':"timestamp:'yyMMddHHmmss'",group:"自动编号属性"},
			          {'text':"timestamp:'yyMMddHHmmssEEE'",group:"自动编号属性"},
			          {'text':"prefix:''",group:"自动编号属性"},
			          {'text':"formid:'f_tableName'",group:"子表属性"},
			          {'text':"addrow:true",group:"动作按钮"},
			          {'text':"delrow:true",group:"动作按钮"}
				    ],
					required: false
				}
		    }},
		    {field:'fieldDataSource',title:'数据源',width:200,editor:{
	    		type: 'textbox', options:{ required: false, validType:['length[0,150]'] }
	     	}}//,{field:'fieldWidth',title:'移动端列宽',width:100}
	    ]],
	    onSelect:function(index, row){
	    	$('#dataGrid').datagrid("beginEdit", index);
	    },
	    onLoadSuccess:function(data){
	    	var lineCount = data.lineCount || 2;
	    	$('#lineCount').combobox('setValue',lineCount);
	    }
	});
});
</script>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',split:false" style="height:100px;">
		<div style="margin-top:5px;margin-left:10px;float:left;">
			<select id="lineCount" class="easyui-combobox" name="lineCount" style="width:200px;" data-options="label:'行列属性:'">   
    			<option value="1">单行1输入</option>   
			    <option value="2" selected="selected">单行2输入</option>   
			    <option value="3">单行3输入</option>  
			    <option value="4">单行4输入</option>  
			    <option value="5">单行5输入</option>
			</select>  
		
			<a id="saveConfigBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保存并刷新</a> 
			<a id="frushConfigBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">刷新设置</a>  
			<a id="viewConfigBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-view'">预览表单</a> 
		 </div>
	</div>   
    <div data-options="region:'center',title:'数据库字段设计'">
    	<table id="dataGrid" style="height: 100%;"></table> 
    </div> 
</body>
</html>
